package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC59 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int[][] generateMatrix(int n) {
            int cur = 1, loop = 0;
            int[][] res = new int[n][n];
            while (cur <= n * n) {// clock wise travel.
                for (int i = loop; cur <= n * n && i < n - loop; i++) res[loop][i] = cur++;
                for (int i = loop + 1; cur <= n * n && i < n - loop; i++) res[i][n - 1 - loop] = cur++;
                for (int i = n - 2 - loop; cur <= n * n && i >= loop; i--) res[n - 1 - loop][i] = cur++;
                for (int i = n - 2 - loop; cur <= n * n && i >= loop + 1; i--) res[i][loop] = cur++;
                ++loop;
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.generateMatrix(4));
    }

    public static void main(String[] args) throws Exception {
        LC59 an = new LC59();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
